package com.javabase.study.ExceptionStudy;

/**
 * TODO
 * 内容：测试学习使用自定义异常类
 * 作者:jxx
 * 创建时间：2021/6/27
 */
public class TestUserDefinateException {
    public static void main(String[] args){

        Person p = new Person();
        p.setName("jiangxx");
        try{
            p.setAge(-1);
        }catch(IllealAgeException i){
            i.printStackTrace();
            System.exit(-1);
        }

        System.out.println(p);
    }
}

class Person{
    private String name;
    private int age;

    public void setName(String name){
        this.name = name;
    }
    public void setAge(int age) throws IllealAgeException {
        if(age < 0){
            //抛出异常
           throw  new IllealAgeException("年龄不能为负数");

        }
        this.age = age;
    }

    public String toString(){
        return "name is " + name + " and age is " + age;
    }

}